<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Post a note news!
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Inherited from Addaction.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_addaction.php';
require_once INSTALLDIR . '/lib/slh_rights.php';

require_once INSTALLDIR . '/widgets/slh_form_addnotenews.php';

require_once INSTALLDIR . '/classes/NoteNews.php';

class AddnotenewsAction extends AddAction
{
	function title()
	{
		return '添加记事新闻';
	}
	
	function prepare($args)
	{
		if ( !Rights::hasRight(slh_get_userrights(), RIGHT_NEWS)) {
			return false;
		}
		
		return parent::prepare($args);
	}
	/**
	 * Add the eaditor stylesheet.
	 */
	function showStylesheets()
	{
		parent::showStylesheets();
		
		$this->cssLink('theme/base/charCount/charCount.css', 'base', 'screen, projection, tv');
	}
	
	/**
	 * Add the eaditor scripts.
	 * Add the charCount
	 */
	function showScripts()
	{
		parent::showScripts();
		
		$this->script('charCount/charCount.js');
	
		$this->script('kindeditor/kindeditor.min.js');
		
		$this->script('action/addnotenews.go.js');
	}
	
	/**
	 * Shows the guidance.
	 * Overriding.
	 */
	function showGuidance()
	{
		// Html code here.
		$this->element('h3', null, '添加一则记事新闻');
		
		$this->instruction('呃,说明下添加的具体是什么东东,怎么添加好了.');
		
		// If added successfully, then?
		if ($this->added) {
			$this->element('p', 'cls_p_success', '添加成功!');
			// Or show a nav.
		}
	}
	
	/**
	 * Shows the widgets like form, or etc.
	 * Overriding.
	 */
	function showForm()
	{
		$form = new AddnotenewsForm($this, $this->args);
		$form->show();
	}	
	
	/**
	 * Try to add the profile into the database.
	 */
	function tryAdd()
	{
		// CSRF protection and Refreshing-post protection.
		$this->checkSessionToken();

		$message	= $this->getTrimmedArg('message');
		$rendered = $this->getTrimmedArg('rendered');
		$user_id = slh_get_userid();
				
// TODO: Validates the data.
		if (empty($message) ||
			mb_strlen($message) > slh_config('notenews', 'message_maxchar')) {
			$this->showAction('新闻简介新闻的内容不合法哦亲,是不是没有内容或者超过字数了?');
		} else if (empty($rendered)) { 
			$this->showAction('新闻总写点什么吧亲?');
		} else {
			// Handle(slashs)/Encode the rendered content.
			/*
			 * If 'magic_quotes_gpc' is on, removes the 'added' slashes '/'. 
			 */
			if (get_magic_quotes_gpc()) {
				$rendered = stripslashes($rendered);
			}
			/*
			// way to encode = slh_compress();
			$compress = gzcompress($rendered);
			$encode = base64_encode($compress);
			// way to decode = slh_uncompress();
			$decode = base64_decode($encode);
			$rendered = gzuncompress($decode);
			*/
			$rendered_encode = slh_compress($rendered);
			
			// Add into the database.
			$notenews_id = NoteNews::add(array('message' => $message,
											   'rendered'=> $rendered_encode,
											   'user_id' => $user_id));
			
			if ( !$notenews_id) {
				$this->showAction('发布新闻失败!');
				return;
			}
			
			// Success.
			// Unset the seesion token preventing post-again.
			$this->unsetSessionToken();
			
			// Unset the seted values.
			unset($this->args['message']);
			unset($this->args['rendered']);
			
			$this->added = true;
			$this->showAction();
		}
	}
}

?>